﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using SYH.Config;
using Common;

namespace SYH.Public.DAL
{
    public class PublicAlbum
    {
        #region 根据相册名称判断是否存在
        /// <summary>
        /// 根据相册名称判断是否存在
        /// </summary>
        /// <param name="name"></param>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public int GetNameExist(string name, int UserId)
        {
            SqlParameter[] par = new SqlParameter[] { 
                new SqlParameter("@Name",name),
                new SqlParameter("@UserId",UserId)
            };
            return Common.TypeParse.ObjectToInt(Common.SqlHelper.ExecuteScalar(WebConfig.SqlConnString, CommandType.StoredProcedure, "Public_Album_NameExists", par), 0);
        }
        #endregion

        #region 增
        /// <summary>
        /// 增加相册
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(Model.PublicAlbum model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Public_Album(");
            strSql.Append("UserId,ImgCount,Name,FrontCover,CreateTime)");
            strSql.Append(" values (");
            strSql.Append("@UserId,@ImgCount,@Name,@FrontCover,@CreateTime)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@UserId", model.UserId),
					new SqlParameter("@ImgCount", model.ImgCount),
					new SqlParameter("@Name", model.Name),
                    new SqlParameter("@FrontCover",model.FrontCover),
					new SqlParameter("@CreateTime", model.CreateTime)};
            return TypeParse.ObjectToInt(SqlHelper.ExecuteScalar(WebConfig.SqlConnString,
                CommandType.Text,
                strSql.ToString(), parameters), 0);
        }
        #endregion

        #region 删
        /// <summary>
        /// 删除一条数据,若此相册有图片将删除失败
        /// </summary>
        public bool Delete(int Id, int UserId)
        {
            return Common.TypeParse.ObjectToInt(SqlHelper.ExecuteScalar(WebConfig.SqlConnString,
                CommandType.StoredProcedure,
                "Public_Album_Delete",
                new SqlParameter("@Id", Id),
                new SqlParameter("@UserId", UserId)), 0) > 0;
        }
        #endregion

        #region 查
        /// <summary>
        /// GetModel
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Model.PublicAlbum GetModel(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,UserId,ImgCount,Name,FrontCover,CreateTime,IsShow  ");
            strSql.Append("  from Public_Album ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            SqlDataReader sdr = Common.SqlHelper.ExecuteReader(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            if (sdr.HasRows)
            {
                Model.PublicAlbum model = new Model.PublicAlbum();
                if (sdr.Read())
                {
                    model.Id = sdr.GetInt32(sdr.GetOrdinal("Id"));
                    model.ImgCount = sdr.GetInt32(sdr.GetOrdinal("ImgCount"));
                    model.Name = sdr.GetString(sdr.GetOrdinal("Name"));
                    model.FrontCover = sdr.GetString(sdr.GetOrdinal("FrontCover"));
                    model.UserId = sdr.GetInt32(sdr.GetOrdinal("UserId"));
                    model.CreateTime = sdr.GetDateTime(sdr.GetOrdinal("CreateTime"));
                    model.IsShow = sdr.GetInt32(sdr.GetOrdinal("IsShow"));
                }
                sdr.Close();
                return model;
            }
            sdr.Close();
            return null;
        }
        #endregion

        #region 改
        /// <summary>
        /// 更新一个字段
        /// </summary>
        /// <param name="name">字段名</param>
        /// <param name="value">值</param>
        /// <param name="strWhere">条件</param>
        /// <param name="flag">字符串表示：true为字符串；false为整型、bool、日期等</param>
        /// <returns></returns>
        public bool UpdateOneField(string name, string value, string strWhere, bool flag)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE Public_Album set ");
            strSql.Append(" " + name + "= ");
            if (flag)
            {
                strSql.Append(" '" + value + "' ");
            }
            else
            {
                strSql.Append(" " + value + " ");
            }
            strSql.Append(" WHERE " + strWhere);

            return SqlHelper.ExecuteSql(WebConfig.SqlConnString, strSql.ToString()) > 0;
        }
        #endregion

        #region 查找会员相册，若未找到将创建“我的相册”，返回0说明当前会员已创建过相册
        /// <summary>
        /// 查找会员相册，若未找到将创建“我的相册”，返回0说明当前会员已创建过相册
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public int ExistsOrAdd(int UserId)
        {
            return Common.TypeParse.ObjectToInt(SqlHelper.ExecuteScalar(WebConfig.SqlConnString,
                           CommandType.StoredProcedure,
                           "Public_Album_ExistsOrAdd",
                           new SqlParameter("@UserId", UserId)), 0);
        }
        #endregion

        #region 根据会员Id查找相册列表
        /// <summary>
        /// 根据会员Id查找相册列表
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public IList<Model.PublicAlbum> GetModelList(int UserId)
        {

            SqlDataReader sdr = Common.SqlHelper.ExecuteReader(WebConfig.SqlConnString,
                CommandType.StoredProcedure,
                "Public_Album_GetModelListByUserId",
                new SqlParameter("@UserId", UserId));
            IList<Model.PublicAlbum> list = new List<Model.PublicAlbum>();
            if (sdr.HasRows)
            {
                Model.PublicAlbum model;
                while (sdr.Read())
                {
                    model = new Model.PublicAlbum();
                    model.Id = sdr.GetInt32(sdr.GetOrdinal("Id"));
                    model.ImgCount = sdr.GetInt32(sdr.GetOrdinal("ImgCount"));
                    model.Name = sdr.GetString(sdr.GetOrdinal("Name"));
                    model.FrontCover = sdr.GetString(sdr.GetOrdinal("FrontCover"));
                    model.UserId = sdr.GetInt32(sdr.GetOrdinal("UserId"));
                    model.CreateTime = sdr.GetDateTime(sdr.GetOrdinal("CreateTime"));
                    list.Add(model);
                }
            }
            sdr.Close();
            return list;
        }
        #endregion

        #region 刷新相册封面
        public void RefeshAlumFontCover(int alumnId)
        {
            SqlParameter[] par = new SqlParameter[] { 
                new SqlParameter("@Id",alumnId)
             };
            Common.SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.StoredProcedure, "Public_Album_RefreshFrontCover", par);
        }
        #endregion

        #region 刷新相册个数
        public void RefeshAlumImageCount(int alumnId)
        {
            SqlParameter[] par = new SqlParameter[] { 
                new SqlParameter("@Id",alumnId)
             };
            Common.SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.StoredProcedure, "Public_Album_RefreshImageCount", par);
        }
        #endregion
    }
}
